package com.stm.bi.etl;

import com.stm.bi.constant.DimensionConstants;
import com.stm.bi.core.util.DateUtils;
import com.stm.bi.enums.DateType;
import com.stm.framework.core.util.FileResourceLoader;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @author liuyx
 * @date 2025/7/17  14:51
 */
@Component
public class AdsResPolicyStyleInventoryEtlTask extends AbstractDataEtlTask{
    @Value("${spring.profiles.active:}")
    private String env;

    @Override
    public boolean isTenantLine() {
        return true;
    }

    @Override
    public boolean syncWithTimeRange() {
        return true;
    }

    @Override
    public String getTaskName() {
        return "ADS政策款式库存结果ETL任务";
    }

    @Override
    public List<DateType> getEtlDateTypes() {
        return Arrays.asList(DateType.YEAR, DateType.MONTH, DateType.WEEK, DateType.DAY);
    }

    @Override
    public int getDelayDays() {
        return 0;
    }

    @Override
    public List<Map> extract(Long orgId, String orgNo, DateType dateType, LocalDateTime startDateTime, LocalDateTime endDateTime) {
        String sql = FileResourceLoader.getResourceAsString("/sql/policyEvolve/adsResPolicyStyleInventory.sql");
        if(env.equals("beta")) {
            sql = FileResourceLoader.getResourceAsString("/sql/policyEvolve/adsResPolicyStyleInventory_beta.sql");
        }

        List<Map> data = new ArrayList<>();

        String date = DateUtils.getDateStrByDateType(startDateTime, dateType);
        List<Map> maps = jdbcService.selectList(sql, new Object[]{orgNo, dateType.getValue(), date, date});

        for (Map map : maps) {
            Map row = new HashMap<>();
            for (Object o : map.entrySet()) {
                Map.Entry entry = (Map.Entry) o;
                row.put(entry.getKey(), entry.getValue());
            }

            row.put("year", String.valueOf(row.get("date")).substring(0, 4));
            data.add(row);
        }
        return data;
    }

    @Override
    public List<Map> transform(Long orgId, String orgNo, List<Map> dataList) {
        return dataList;
    }
    @Override
    public void load(Long orgId, String orgNo, List<Map> dataList) {
        dorisLoadRepository.putLoad("ads_res_policy_style_inventory",dataList);
    }
}
